package com.lanxin.zc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanxin.bx.entity.BxInformaldetail;
import com.lanxin.bx.entity.BxInformaltype;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.bx.vo.AllinformalVo;
import com.lanxin.zc.entity.ZcAsset;
import com.lanxin.zc.entity.ZcAssettype;
import com.lanxin.zc.service.IZcAssetService;
import com.lanxin.zc.service.IZcAssettypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-07-16
 */
@Controller
@RequestMapping("/zc-assettype")
public class ZcAssettypeController {

    @Autowired
    private IZcAssettypeService zcAssettypeService;
    @Autowired
    private IZcAssetService zcAssetService;

    @RequestMapping("/tozcassettypelist")
    public ModelAndView tozcassettypelist(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "typeName", required = false) String typeName) {

        Page<ZcAssettype> zcAssettypePage = new Page<>(page, pageSize);
        QueryWrapper<ZcAssettype> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(typeName) && !typeName.isEmpty()) {
            wrapper.like("typeName", typeName);
        }

        Page<ZcAssettype>  zcAssettypelist= zcAssettypeService.page(zcAssettypePage, wrapper);


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("zc/zcassettype/zcassettypelist");


        modelAndView.addObject("zcAssettypelist",zcAssettypelist);
        modelAndView.addObject("total", zcAssettypelist.getTotal()); // 总记录数
        modelAndView.addObject("pageNum", zcAssettypelist.getCurrent()); // 当前页码
        modelAndView.addObject("pageSize", zcAssettypelist.getSize()); // 每页显示条数
        modelAndView.addObject("totalPages", zcAssettypelist.getPages()); // 总页数
        modelAndView.addObject("typeName", typeName); // 保留搜索条件（回显）

        return modelAndView;
    }

    @RequestMapping("/deleteAssettype")
    @ResponseBody
    public boolean deleteAssettype(@RequestParam Integer oid){

        QueryWrapper<ZcAsset> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",oid);
        long count = zcAssetService.count(queryWrapper);
        if(count > 0){
            return false;
        }
        return zcAssettypeService.removeById(oid);

    }

    @RequestMapping("/tozcassetypeedit")
    public ModelAndView tozcassetypeedit(@RequestParam(required = false)Integer oid){

        ModelAndView modelAndView = new ModelAndView();
        if (oid != null) {
            ZcAssettype zcAssettype = zcAssettypeService.getById(oid);
            if (zcAssettype != null) {
                modelAndView.addObject("zcAssettype", zcAssettype); // 将对象传递给页面
            }
        }
        modelAndView.setViewName("zc/zcassettype/zcassettypeedit");
        return modelAndView;

    }

    @RequestMapping("/checkAssettypeName")
    @ResponseBody
    public Map<String, Boolean> checkAssettypeName(String typeName, Integer oid) {
        QueryWrapper<ZcAssettype> wrapper = new QueryWrapper<>();
        wrapper.eq("typeName", typeName);
        if (oid != null) {
            wrapper.ne("oid", oid);
        }
        long count = zcAssettypeService.count(wrapper);

        // 构造JSON对象（包含valid字段）
        Map<String, Boolean> result = new HashMap<>();
        result.put("valid", count == 0);
        return result;
    }

    @RequestMapping("/Assettypeedit")
    public String Assettypeedit(ZcAssettype zcAssettype, Integer oid){

        ModelAndView modelAndView = new ModelAndView();
        if (zcAssettype.getStatus() == null || zcAssettype.getStatus().trim().isEmpty()) {
            zcAssettype.setStatus("Y");
        }
        if (zcAssettype.getOid() != null) {
            zcAssettypeService.updateById(zcAssettype);
        } else {
            zcAssettypeService.save(zcAssettype);
        }
        modelAndView.setViewName("bx/informaltype/informaltypeedit");
        return "redirect:/zc-assettype/tozcassettypelist";

    }


}
